//
//  QRCodeCardViewController.swift
//  JKWeiBo
//
//  Created by 王冲 on 2017/6/15.
//  Copyright © 2017年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

class QRCodeCardViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        // 1. 设置标题
        view.addSubview(tittleLabel)
        view.addSubview(backClick)
        // 2. 添加图片容器
        view.addSubview(icon)
        
        // 3. 布局图片容器
        
        // 4. 生成二维码
        let qrcodeImage = createQrimage()
        
        // 5. 将生成好的二维码添加到容器上
        icon.image = qrcodeImage
    }
    
     private func createQrimage() -> UIImage {
        // 1.创建滤镜 // CIQRCodeGenerator 不能写错
        let filter = CIFilter(name: "CIQRCodeGenerator")
        // 2.还原滤镜的默认属性
        filter?.setDefaults()
        // 3.设置需要生成的二维码数据
        filter?.setValue("joanking".data(using: String.Encoding.utf8), forKey: "inputMessage")
        // 4.取出生成好的图片
        let ciImage = filter?.outputImage
        let backGroungImage = createNonInterpolatedUIImageFormCIImage(image: ciImage!, size: 200)
        // 5.创建一个头像
        let icon = UIImage(named: "6.jpg")
        // 6.合成图片(将二维码和图片进行合并)
        let newImage = createImage(bgImage: backGroungImage, iconImage: icon!)
        // 7.返回生成好的二维码
        return newImage
    }
    
    // MARK: 合成图片(将二维码和图片进行合并)
    private func createImage(bgImage: UIImage,iconImage: UIImage) -> UIImage{
    
        // 1.开启图文上下文
        UIGraphicsBeginImageContext(bgImage.size)
        // 2.绘制背景图片
        bgImage.draw(in: CGRect(origin: CGPoint.zero, size: bgImage.size))
        // 3.绘制头像
        let width1: CGFloat = 50
        let height1: CGFloat = width1
        let x = (bgImage.size.width - width1)*0.5
        let y = (bgImage.size.height - height1)*0.5
        iconImage.draw(in: CGRect(x: x, y: y, width: width1, height: height1))
        // 4.取出绘制好的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        // 5.关闭上下文
        UIGraphicsEndImageContext()
        // 6.返回合成好的图片
        return newImage!
         
    }

    //MARK: - 根据CIImage生成指定大小的高清UIImage
    func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        //CIImage没有frame与bounds属性,只有extent属性
        let ciextent: CGRect = image.extent.integral
        let scale: CGFloat = min(size/ciextent.width, size/ciextent.height)
        
        let context = CIContext(options: nil)  //创建基于GPU的CIContext对象,性能和效果更好
        let bitmapImage: CGImage = context.createCGImage(image, from: ciextent)! //CIImage->CGImage
        
        let width = ciextent.width * scale
        let height = ciextent.height * scale
        let cs: CGColorSpace = CGColorSpaceCreateDeviceGray() //灰度颜色通道
        let info_UInt32 = CGImageAlphaInfo.none.rawValue
        
        let bitmapRef = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: info_UInt32)! //图形上下文，画布
        bitmapRef.interpolationQuality = CGInterpolationQuality.none //写入质量
        bitmapRef.scaleBy(x: scale, y: scale) //调整“画布”的缩放
        bitmapRef.draw(bitmapImage, in: ciextent)  //绘制图片
        
        let scaledImage: CGImage = bitmapRef.makeImage()! //保存
        return UIImage(cgImage: scaledImage)
    }
    

    // MARK: 添加二维码图片的容器
    private lazy var icon: UIImageView = {
    
        let imageview = UIImageView()
        imageview.frame = CGRect(x: JKscreenW/2.0-100, y: JKscreenH/2.0-100, width: 200, height: 200)
        imageview.backgroundColor = UIColor.brown
        return imageview
    
    }()
    
    // MARK: 添加标题
    private lazy var tittleLabel: UILabel = {
    
        let tittle = UILabel(frame: CGRect(x: JKscreenW/2.0-100, y: 34, width: 200, height: 16))
        tittle.text = "我的名片"
        tittle.font = UIFont.systemFont(ofSize: 16)
        tittle.textAlignment = NSTextAlignment.center
        tittle.textColor = UIColor.JKTextGayColor()
        return tittle
    }()
    
    // MARK: 添加返回按钮
    private lazy var backClick: UIButton = {
    
        let back = UIButton(frame: CGRect(x: 12, y: 33, width: 40, height: 18))
        back.setTitle("返回", for: UIControlState.normal)
        back.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        back.setTitleColor(UIColor.black, for: UIControlState.normal)
        //back.layer.cornerRadius = 2
        //back.backgroundColor = UIColor.JKTextGayColor()
        back.addTarget(self, action: #selector(QRCodeCardViewController.backBtn), for: UIControlEvents.touchUpInside)
        return back
    
    }()
    
    // MARK: 返回的点击事件
    func backBtn() {
        
        dismiss(animated: true, completion: nil)
        
    }
 
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
        
    }

}
